$(document).ready(function(){
    query(1);
});
function query(pageNum) {
    $.post(
        "/admin/queryRoles",
        {
            pageNum:pageNum
        },
        function(data,status){

            var $body = $("table.role-table > tbody");
            var list = data.list;
            var content = '';
            for(var i=0;i<list.length;i++){
                var node = list[i];
                content = content+'<tr>';
                content = content+'<td>'+(i+1)+'</td>';
                content = content+'<td>'+node.id+'</td>';
                content = content+'<td>'+node.roleName+'</td>';
                content = content+'<td><span class="power"><a href="/admin/editRoleList/'+node.id+'"><i class="fa fa-gear"></i>权限配置</a></span><span class="roler" onclick="deleteNode('+node.id+')"><i class="fa fa-trash-o"></i>删除角色</span></td>';
                content = content+'</tr>';
            }
            $body.html(content);
            //分页

            var totalCount = data.total;
            var totalPage = data.pages;
            console.log('totalCount:'+totalCount);
            var pageNum = data.pageNum;
            var start = data.start;
            var end = data.end;
            $("#totalPage").val(totalPage);
            $("#pageNum").val(pageNum);
            console.log('pageNum:'+pageNum);
            if(totalPage === 0){
                $(".no-data").css("display","block");
                $(".page-list").hide();
            }else {
                $(".no-data").hide();
                // $(".page-list").show();
                var htmlContent = '';
                htmlContent = htmlContent+'<span id="totalCount">共'+totalCount+'条</span>';
                if((pageNum-start)>0){
                    htmlContent = htmlContent+'<a href="javascript:queryByPageNum('+(pageNum-1)+');">上一页</a>';
                }
                for(var index = start;index<=end;index++){
                    htmlContent = htmlContent+'<a '+((pageNum===index)?'class="active "':'')+' href="javascript:queryByPageNum('+index+');">'+index+'</a>';
                }
                if((end-pageNum)>0){
                    htmlContent = htmlContent+'<a href="javascript:queryByPageNum('+(pageNum+1)+');">下一页</a>';
                }
                htmlContent = htmlContent+'<span>跳转到<input id="jumpPage" type="number" value="">页</span>';
                htmlContent = htmlContent+'<span class="jump" onclick="queryByJump()" >跳转</span>';
                $(".page-list").show().html(htmlContent);
            }



            // alert("数据: \n" + data + "\n状态: " + status);
        });
}

function queryByPageNum(currentPageNum) {
    query(currentPageNum);
}
function queryByJump(){
    var jumpPage = $("#jumpPage").val();
    // alert('test:');
    var totalPage = $("#totalPage").val();
    var pageNum = $("#pageNum").val();
    if(typeof(jumpPage) !== 'number' && (jumpPage>totalPage || jumpPage<1)){
        alert("请输入合法页码!");
        return;
    }
    query(jumpPage);
}
function deleteNode(roleId) {
    layer.open({
        type: 1,
        area: ['300px', '200px'],
        shadeClose: true, //点击遮罩关闭
        content: '<div style="padding:20px;"><p>确定删除？</p><p>删除角色后，该角色下面的用户会自动变为默认角色！</p></div>',
        btn:['确定','取消'],
        yes: function(index){
            layer.close(index); //如果设定了yes回调，需进行手工关闭
            $.get("/admin/role/delete/"+roleId,function (data) {
                if(data.status){
                    // alert('删除成功!');
                    window.location.href ='/admin/roleList';
                }else{

                }
            });
        }
    });
}